package com.zhuoyue.web.controller.v2;

import com.zhuoyue.biz.core.api.domain.AppUserHomeRelationDO;
import com.zhuoyue.biz.core.api.service.impl.AppUserHomeRelationService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;
import com.zhuoyue.common.aop.code.SmsCode;
import com.zhuoyue.common.aop.phone.Phone;
import com.zhuoyue.common.dto.ControllerResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;


/**
 * @author wjming
 * @ClassName: AppUserHomeRelationController
 * @Description: 房间家庭关系控制接口
 * @Date 2021/4/17 12:42
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/user/home/relation")
@Validated
@Slf4j
public class AppUserHomeRelationV2Controller {
    @Autowired
    private AppUserHomeRelationService appUserHomeRelationService;


    /***
     * 把家庭分享给子账号
     * @param request 用户信息
     * @param homeId 用户的家庭ID
     * @param childAccount 被分享者的ID
     * @return
     */
    @PostMapping(value = "/addChildUser")
    @CheckLogin
    @SmsCode(requestPhoneValue = "childAccount")
    public ControllerResult createChildAccount(HttpServletRequest request, String homeId, String childAccount) {
        User user = (User) request.getAttribute("user");
        String organizationCode = user.getOrgCode();
        appUserHomeRelationService.createChildAccount(user.getId(), homeId, childAccount, organizationCode);
        return ControllerResult.toReturnMessageSuccess("分享成功");
    }

    /**
     * 取消分享
     *
     * @param request
     * @param homeId       取消的家庭ID
     * @param childAccount 子账号
     * @return
     */
    @DeleteMapping("/delChildUser")
    @CheckLogin
    public ControllerResult removeChildAccount(HttpServletRequest request, @NotBlank(message = "家庭ID不能为空") String homeId,
                                               @Phone String childAccount) {
        User user = (User) request.getAttribute("user");
        String account = user.getAccount();
        appUserHomeRelationService.cancelShare(homeId, childAccount, user.getOrgCode(), account.equals(childAccount) ? "1" : "0");
        return ControllerResult.toReturnMessageSuccess("取消分享成功");

    }

    /**
     * 分享账号列表
     *
     * @param homeId
     * @return
     */
    @PostMapping("/childUserList")
    @CheckLogin
    public ControllerResult getChildUserList(@NotBlank(message = "家庭ID不能为空!") String homeId) {
        return ControllerResult.toReturnDataSuccess("获取成功", appUserHomeRelationService.getChildUserList(homeId));
    }


    /**
     * 家庭转移
     *
     * @param request
     * @param homeId
     * @param transferAccount
     * @return
     */
    @PutMapping("/transferHome")
    @CheckLogin
    @SmsCode(requestPhoneValue = "transferAccount")
    public ControllerResult familyTransfer(HttpServletRequest request,
                                           @NotBlank(message = "家庭ID不能为空!") String homeId,
                                           @NotBlank(message = "转移账号不能为空") String transferAccount) {


        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [familyTransfer] -> [homeId:{},transferAccount{}]", user.getAccount(), user.getOrgCode(), homeId, transferAccount);
        String organizationCode = user.getOrgCode();
        // 1.通过appUserId和HomeId之间的关系，然后修改当前homeId下的appUserId
        AppUserHomeRelationDO appUserHomeRelationDO = new AppUserHomeRelationDO().addMasterRelation(user.getId(), homeId);
        appUserHomeRelationService.transferHome(transferAccount, appUserHomeRelationDO, organizationCode);
        return ControllerResult.toReturnMessageSuccess("转移成功");

    }
}
